package controller;

import java.sql.SQLException;

import javax.swing.JOptionPane;

import ui.MessagePopUp;
import ui.Screen;
import data.Account;

/** Representing all user menu related actions, 
 * used to insert parameters in the data package
 * in order to manipulate the data of the table db_account
 * in the database and in the arraylist accountlist
 * @author Matthias
 *
 */
public class ATMUserMenuControl {

	/** Declaring the ATM object **/
	private final ATM atm;

	/** Constructor of ATMUserMenuControl
	 * @param atm is an ATM object
	 */
	public ATMUserMenuControl(final ATM atm) {
		this.atm = atm;
	}

	/**
	 * Method is used to adding an account to the user
	 */
	public final void userMenuAdd() {

		String accountType = null;

		int accNr = 0;
		// get the input from the textfield, a 6 digit
		accNr = atm.getClassScreen().getUserMenuAdd().getAccNr();
		String checkNr = Integer.toString(accNr);

		// checking if the input is ok in the regex
		// if right, continue, otherwise message prompt
		boolean rightInput = atm.getClassBankDatabase().
				getClassRegexInput().isSixDigits(checkNr);

		if (accNr <= 0 || rightInput == false) {
			accountType = null;
		}
		// get the input of the Credit rdbtn if selected
		else if (atm.getClassScreen().getUserMenuAdd().getRdbtnCredit().isSelected() && accNr > 0) {
			// if no credit account is available, the user can add a credit account
			if (atm.getClassBankDatabase().getClassUser().getAccountList().isEmpty()) {
				accountType = "Credit";
			} else {
				// looping if arraylist accountlist already contains a credit account
				// if exists -> stop here
				for (Account s : atm.getClassBankDatabase().getClassUser().getAccountList()) {
					if (s.getAccountType().equals("Credit")) {
						accountType = null;
						break;
					}
					// doesn't exist -> type is credit
					else if (!s.getAccountType().equals("Credit")) {
						accountType = "Credit";
					}
				}
			}
		}
		// get the input of the Savings rdbtn if selected
		else if (atm.getClassScreen().getUserMenuAdd().getRdbtnSavings().isSelected() && accNr > 0) {
			// if no Savings account is available, the user can add a Savings account
			if (atm.getClassBankDatabase().getClassUser().getAccountList().isEmpty()) {
				accountType = "Savings";
			} else {
				// looping if arraylist accountlist already contains a Savings account
				// if exists -> stop here
				for (Account s : atm.getClassBankDatabase().getClassUser().getAccountList()) {
					if (s.getAccountType().equals("Savings")) {
						accountType = null;
						break;
					}
					// doesn't exist -> type is Savings
					else if (!s.getAccountType().equals("Savings")) {
						accountType = "Savings";
					}

				}
			}
		}
		// get the input of the Depot rdbtn if selected
		else if (atm.getClassScreen().getUserMenuAdd().getRdbtnDepot().isSelected()  && accNr > 0) {
			// if no Depot account is available, the user can add a Depot account
			if (atm.getClassBankDatabase().getClassUser().getAccountList().isEmpty()) {
				accountType = "Depot";
			} else {
				// looping if arraylist accountlist already contains a Depot account
				// if exists -> stop here
				for (Account s : atm.getClassBankDatabase().getClassUser().getAccountList()) {
					if (s.getAccountType().equals("Depot")) {
						accountType = null;
						break;
					}
					// doesn't exist -> type is Depot
					else if (!s.getAccountType().equals("Depot")) {
						accountType = "Depot";
					}
				}
			}
		}
		// prompting possible error message, if wrong input
		// -> accounttype is null
		if (accountType == null) {
			String text = "Adding not possible! " 
					+ "Either you already one account of type" 
					+ "or the input is not matching the requirement. "
					+ "\nPlease try again";
			new MessagePopUp().setError(text);
		}
		// if accounttype is not null it is added to the database
		else if (accountType != null) {
			try {
				// get the current user id
				int userID = atm.getClassBankDatabase().getClassUser().getUserID();
				String user = String.valueOf(userID);
				
				double totalBalance = 0;
				// get the matching interest rate of the account type
				String interestRate = atm.getClassBankDatabase().getInterestRate(accountType);
				// calling the method to insert the account into the table
				atm.getClassBdbAccSQL().insertAccountIntoTable(user, accNr, accountType,
						interestRate, totalBalance);
				// clearing the fields after adding
				atm.getClassScreen().getUserMenuAdd().clearFields();
				if (atm.getClassBankDatabase().getUsers(userID).getAccountList().size() == 3) {
					atm.getClassScreen().show(Screen.USERMENU);
					String text = "You have now 3 Accounts, no more possible!";
					new MessagePopUp().setError(text);
				}
			} catch (SQLException exc) {
				String text = "Adding not possible! " 
						+ "Either you already one account of type" 
						+ "or the input is not matching the requirement. "
						+ "Please try again";
				new MessagePopUp().setError(text);
			}
		}
	}

	/**
	 * Method for deleting a selected account in the 
	 * UserMenuDelete
	 */
	public final void userMenuDelete() {
		try {
			// getting the selected account number
			int accNr = atm.getClassScreen().getUserMenuDelete().getSpecificAccNr();
			// restricting the user to delete an account if, the selected
			// account has a balance above 0
			if (atm.getClassBankDatabase().getUserMenuDelTotalBalance(
					atm.getClassScreen().getWelcome().getLoginUserID()) > 0) {
				String text = "Delete not possible, since the account" 
						+ " contains a balance over 0";
				new MessagePopUp().setError(text);
			}
			else {
				int selectedOption = JOptionPane.showConfirmDialog(
						null, "Are you sure you want to delete the account?", 
								"YES / NO", JOptionPane.YES_NO_OPTION); 
				if (selectedOption == JOptionPane.YES_OPTION) {
					// deleting the account from the database
					atm.getClassBdbAccSQL().deleteAccountFromTable(accNr);
					// refreshing the JComboBox
					atm.getClassBankDatabase().populateSQLAccountTable();
					atm.getClassScreen().getUserMenuDelete().addAllAcc();
				}
			}
		} catch (SQLException exc) {
			String text = "Deleting not possible! Please try again";
			new MessagePopUp().setError(text);
		}
	}

} // end this class